<template>
	<view>
		<tn-nav-bar :isBack='true' :backTitle='" "' bottomShadow :alpha="alpha">我的地址</tn-nav-bar>
		<image src="/static/product/topbg.png" class="topbg" mode=""></image>
		<view :style="{paddingTop: vuex_custom_bar_height + 'px'}" class="con">
			<view class="con__box">
				<image src="/static/my/dibg.png" class="dibg" mode=""></image>
				<view class="list-title-container">
					<image src="/static/my/dzicon.png" class="list-title-container-icon" mode=""></image>
					收件人
				</view>
				<!-- 表单 -->
				<tn-form :model="form" ref="form" :errorType="['message','toast']" :labelWidth='136'
					:labelStyle='{color:"#6D6D6D",fontSize:"26rpx"}'>
					<tn-form-item label="姓名" prop="username">
						<tn-input v-model="form.username" placeholder='请输入真实姓名'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
					<tn-form-item label="手机号" prop="mobile">
						<tn-input v-model="form.mobile" placeholder='请输入手机号'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
					<tn-form-item label="地区">
						<tn-input v-model="addr" placeholder='请输入省.市.区'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' type='select' @click="show = true" />
					</tn-form-item>
					<tn-form-item label="详情地址" prop="detail">
						<tn-input v-model="form.detail" placeholder='请详细到街道小区名'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
				</tn-form>
				<view class="tn-flex tn-flex-row-center" style="padding: 50rpx 0;">
					<tn-button :loading='btnloading' fontColor="#FFFFFF" @click="submit" shape='round' width='604rpx' height='99rpx' :fontSize='36'
						backgroundColor="tn-bg-lv" shadow>确认使用</tn-button>
				</view>
			</view>
			 <tn-picker mode="region" v-model="show" :areaCode='["44", "4401", "440106"]' @confirm='confirm' @cancel='show=false'></tn-picker>

		</view>
	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {
		data() {
			return {
				alpha: true,
				form: {
					mobile: '',
					username: '',
					detail: '',
					province:'',
					city:'',
					area:'',
					
				},
				addr: '请选择省.市.区',
				show:false,
				rules: {
					username: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
					mobile: [{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// this.$tn.test.mobile()就是返回true或者false的
							return this.$tn.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: ['change', 'blur'],
					}],
					detail:[
						{
							required: true,
							message: '请输入详细地址',
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['blur'],
						}
					]
				},
				btnloading:false,
				lists: []
			};
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			submit() {
				let _this=this
				this.$refs.form.validate(valid => {
					if (valid) {
						if(!this.form.area){
							this.$tn.message.toast('请选择地区')
							return
						}
						this.btnloading=true
						user.insertAddress(this.form).then(res=>{
							if(res.data.code==1){
								this.$tn.message.toast('使用成功')
								setTimeout(()=>{
									_this.btnloading=false
									uni.navigateBack()
								},1000)
							}else{
								_this.btnloading=false
							}
						})
					} else {
						console.log(valid);
						console.log('验证失败');
					}
				});
			},
			confirm(e){
				this.addr=e.province.label+e.city.label+e.area.label
				this.form.province=e.province.label
				this.form.city=e.city.label
				this.form.area=e.area.label
				this.show=false
			},
		}
	}
</script>

<style lang="scss">
	.con {
		padding: 30rpx;
		position: relative;

		&__box {
			width: 690rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 3rpx 57rpx 0rpx rgba(0, 0, 0, 0.04);
			border-radius: 30rpx;
			margin-top: 50rpx;
			// overflow: hidden;
			padding-left: 30rpx;
			padding-bottom: 10rpx;
			position: relative;

			.list-title-container {
				display: flex;
				align-items: center;
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #020202;
				line-height: 30rpx;
				padding: 48rpx 0 20rpx 0;

				&-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}
			}
		}
	}

	.tn-border-solid-bottom {
		border-color: #F9F9F9;
		border-bottom-width: 2rpx !important;
	}

	.tn-form-item {
		line-height: 72rpx;
		padding: 28rpx 10rpx 28rpx 0;

		/deep/.tn-input__input {
			font-size: 28rpx;
		}
	}

	.tn-image-upload__custom-btn {
		width: 284rpx;
		height: 168rpx;
		background: rgba(154, 154, 154, .1);
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #9A9A9A;
		line-height: 24rpx;

		.up {
			width: 46rpx;
			height: 46rpx;
			margin-bottom: 26rpx;
		}
	}

	.checkbox {
		width: 28rpx;
		height: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 9rpx;

		&__icon {
			width: 28rpx;
			height: 28rpx;
		}
	}

	.xieyi {
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #888888;
		line-height: 24rpx;
		margin: 41rpx 0 54rpx;
		padding-left: 5rpx;
	}

	.dibg {
		position: absolute;
		top: -51rpx;
		right: 33rpx;
		width: 139rpx;
		height: 139rpx;
	}
</style>